package com.corecloud.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corecloud.entity.SysDepartmentEntity;
import com.corecloud.service.SysDepartmentService;
import com.corecloud.tools.AjaxResultData;
import com.corecloud.tools.PageDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * 教育主管单位管理相关
 */
@Controller
@RequestMapping("/eduManage")
public class DepartmentController {
    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 教育主管单位首页
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("eduManage/index");
        return modelAndView;
    }


    /**
     * 教育主管单位页数据（分页）
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Page<SysDepartmentEntity> list(SysDepartmentEntity department, PageDemo pageDemo){
        QueryWrapper<SysDepartmentEntity> qw = new QueryWrapper();
        //拼装查询条件
        if(!StringUtils.isEmpty(department.getType())){
            qw.eq("type",department.getType());
        }
        if(!StringUtils.isEmpty(department.getName())){
            qw.like("name",department.getName());
        }
        OrderItem oi = new OrderItem();
        oi.setAsc(true);
        oi.setColumn("department_id");
        pageDemo.addOrder(oi);
        Page<SysDepartmentEntity> infoByPage = sysDepartmentService.getInfoByPage(pageDemo, qw);
        List<SysDepartmentEntity> records =infoByPage.getRecords();
        records.forEach(de->{
            if (!StringUtils.isEmpty(de.getDepartmentParentId())){
                de.setDepartmentParenName(sysDepartmentService.findNameById(de.getDepartmentParentId()));
            }else {
                de.setDepartmentParenName("/");
            }
        });
        infoByPage.setRecords(records);
        return infoByPage;
    }

    @GetMapping("/all")
    @ResponseBody
    public AjaxResultData getAll(){
        List<SysDepartmentEntity> listByParas = sysDepartmentService.getAll();
        return new AjaxResultData(200,"成功",listByParas);
    }

}
